package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.NewDateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应收表Controller
 * 
 * @author zhangjun
 * @date 2020-02-14
 */
@Controller
@RequestMapping("/system/yingshou")
public class AlpYingshouController extends BaseController
{
    private String prefix = "system/yingshou";

    @Autowired
    private IAlpYingshouService alpYingshouService;

    @Autowired
    private IAlpContractDetailService alpContractDetailService;

    @Autowired
    private IAlpContractStageService alpContractStageService;

    @Autowired
    private IAlpContractStageDetailService alpContractStageDetailService;


    @RequiresPermissions("system:yingshou:view")
    @GetMapping()
    public String yingshou()
    {
        return prefix + "/yingshou";
    }


    /**
     * 查询应收表列表
     */
    @RequiresPermissions("system:yingshou:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpYingshou alpYingshou)
    {
        startPage();
        System.out.println("&&&&&&&&&&&&&&&&&&&");
        System.out.println(JSONObject.toJSONString(alpYingshouService.calcBPAatNow()));

        List<AlpYingshou> yingShouList = new ArrayList<AlpYingshou>();
        AlpContractDetail alpContractDetail = new AlpContractDetail();
        List<AlpContractDetail> list = alpContractDetailService.selectAlpContractDetailList(alpContractDetail);
        for (AlpContractDetail contractDetail : list) {
            //根据合同id 查出本合同的阶段表内容
            List<AlpContractStage> contractStages = alpContractStageService.selectAlpContractStageByContractId(contractDetail.getContractId());
            for (AlpContractStage contractStage : contractStages) {
                AlpYingshou yingshou = new AlpYingshou();
                //根据阶段id 查出阶段最新的预测预算实际
                List<AlpContractStageDetail> stageDetails = alpContractStageDetailService.selectAlpContractStageDetailByStageId(contractStage.getStageId());
                contractStage.setAlpContractStageDetails(stageDetails);


                yingshou.setDepartment(contractDetail.getXsDepartment());
                yingshou.setStageId(contractStage.getStageId());
                yingshou.setStageName(contractStage.getStageName());
                yingshou.setSalesmanFirst(contractStage.getStagePrincipal1());
                yingshou.setSalesmanSecond(contractStage.getStagePrincipal2());
                yingshou.setCollectionCompany(contractDetail.getBiddingCompany());//逻辑错误
                yingshou.setCustomerName(contractDetail.getPartyAName());
                yingshou.setContractNo(contractDetail.getContractNo());
                yingshou.setContractName(contractDetail.getContractName());
                yingshou.setContractMoney(contractDetail.getBiddingMoney());



                yingShouList.add(yingshou);
            }

        }

        return getDataTable(yingShouList);
    }

    public void sss() {
        //先统计出所有合同对象--负责人--部门
        //为合同中放入阶段

        //得到 合同对象（包含本合同的各个预算、预测、实际  金额和时间）

        //分组 合同对象  --以负责人分组
           //组内统计 以月度为维度  预算、预测、实际  完成率


    }

    /**
     * 导出应收表列表
     */
    @RequiresPermissions("system:yingshou:export")
    @Log(title = "应收表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpYingshou alpYingshou)
    {
        List<AlpYingshou> list = alpYingshouService.selectAlpYingshouList(alpYingshou);
        ExcelUtil<AlpYingshou> util = new ExcelUtil<AlpYingshou>(AlpYingshou.class);
        return util.exportExcel(list, "yingshou");
    }

    /**
     * 新增应收表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存应收表
     */
    @RequiresPermissions("system:yingshou:add")
    @Log(title = "应收表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpYingshou alpYingshou)
    {
        return toAjax(alpYingshouService.insertAlpYingshou(alpYingshou));
    }

    /**
     * 修改应收表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AlpYingshou alpYingshou = alpYingshouService.selectAlpYingshouById(id);
        mmap.put("alpYingshou", alpYingshou);
        return prefix + "/edit";
    }

    /**
     * 修改保存应收表
     */
    @RequiresPermissions("system:yingshou:edit")
    @Log(title = "应收表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpYingshou alpYingshou)
    {
        return toAjax(alpYingshouService.updateAlpYingshou(alpYingshou));
    }

    /**
     * 删除应收表
     */
    @RequiresPermissions("system:yingshou:remove")
    @Log(title = "应收表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpYingshouService.deleteAlpYingshouByIds(ids));
    }

    /**
     * 统计页面
     */
    @RequiresPermissions("system:yingshou:view")
    @GetMapping("/statistic")
    public String statistic()
    {
        return prefix + "/statistic";
    }

    @GetMapping("/statistic1")
    @ResponseBody
    public AjaxResult statistic1()
    {
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.showActualState()));
    }

    @GetMapping("/statistic2")
    @ResponseBody
    public AjaxResult statistic2()
    {
        //String jsonStr = "{\"1季度\":[800,800,600],\"2季度\":[0,0,200],\"3季度\":[0,0,0],\"4季度\":[0,0,0]}";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcQuarterSum()));
    }

    @GetMapping("/statistic3")
    @ResponseBody
    public AjaxResult statistic3()
    {
        //String jsonStr = "{\"1月\":[800,0,0],\"2月\":[0,700,400],\"3月\":[0,100,200],\"4月\":[0,0,100],\"5月\":[100,100,100],\"6月\":[100,100,0],\"7月\":[0,0,200],\"8月\":[0,0,0],\"9月\":[0,0,0],\"10月\":[0,0,0],\"11月\":[0,0,0],\"12月\":[0,0,0]}";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcMonthSum()));
    }

    @GetMapping("/statistic4")
    @ResponseBody
    public AjaxResult statistic4()
    {
        //String jsonStr = "[{\"booking\":1600,\"params\":{},\"xsPrincipalName\":\"张三\"},{\"booking\":300,\"params\":{},\"xsPrincipalName\":\"王五\"},{\"booking\":500,\"params\":{},\"xsPrincipalName\":\"李四\"}]";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.everyoneSumBooking()));
    }

    @GetMapping("/statistic5")
    @ResponseBody
    public AjaxResult statistic5()
    {
        //String jsonStr = "[{\"contractCount\":2,\"contractNumber\":5500,\"params\":{},\"showType\":\"已完工应收金额\"},{\"contractCount\":4,\"contractNumber\":0,\"params\":{},\"showType\":\"未达回款状态金额\"},{\"contractCount\":5,\"contractNumber\":2800,\"params\":{},\"showType\":\"合同总数\"},{\"contractCount\":4,\"contractNumber\":46044,\"params\":{},\"showType\":\"执行中合同\"},{\"contractCount\":0,\"params\":{},\"showType\":\"已完工未回款\"},{\"contractCount\":0,\"params\":{},\"showType\":\"已完工已回款\"}]";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcContractSheet()));
    }

    @GetMapping("/statistic6")
    @ResponseBody
    public AjaxResult statistic6()
    {
        //String jsonStr = "[{\"contractCount\":2,\"contractNumber\":5500,\"params\":{},\"showType\":\"已完工应收金额\"},{\"contractCount\":4,\"contractNumber\":0,\"params\":{},\"showType\":\"未达回款状态金额\"},{\"contractCount\":5,\"contractNumber\":2800,\"params\":{},\"showType\":\"合同总数\"},{\"contractCount\":4,\"contractNumber\":46044,\"params\":{},\"showType\":\"执行中合同\"},{\"contractCount\":0,\"params\":{},\"showType\":\"已完工未回款\"},{\"contractCount\":0,\"params\":{},\"showType\":\"已完工已回款\"}]";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcOverDueContract()));
    }

    @GetMapping("/statistic7")
    @ResponseBody
    public AjaxResult statistic7()
    {
        //String jsonStr = "{\"1周\":[500,0,0],\"2周\":[0,0,0],\"3周\":[0,0,0],\"4周\":[0,0,0],\"5周\":[0,600,0],\"6周\":[0,100,0],\"7周\":[0,0,200],\"8周\":[500,0,890],\"9周\":[0,1000,500],\"10周\":[0,0,0],\"11周\":[0,0,0],\"12周\":[1000,500,0],\"13周\":[0,0,100],\"14周\":[0,0,0],\"15周\":[0,0,0],\"16周\":[0,0,0],\"17周\":[100,100,50],\"18周\":[0,0,0],\"19周\":[0,0,0],\"20周\":[0,0,0],\"21周\":[0,0,0],\"22周\":[100,100,0],\"23周\":[0,0,0],\"24周\":[0,0,0],\"25周\":[0,0,0],\"26周\":[0,0,200],\"27周\":[0,0,0],\"28周\":[0,0,0],\"29周\":[0,0,0],\"30周\":[0,0,0],\"31周\":[0,0,0],\"32周\":[0,0,0],\"33周\":[0,0,0],\"34周\":[0,0,0],\"35周\":[0,0,0],\"36周\":[0,0,0],\"37周\":[0,0,0],\"38周\":[0,0,0],\"39周\":[0,0,0],\"40周\":[0,0,0],\"41周\":[0,0,0],\"42周\":[0,0,0],\"43周\":[0,0,0],\"44周\":[0,0,0],\"45周\":[0,0,0],\"46周\":[0,0,0],\"47周\":[0,0,0],\"48周\":[0,0,0],\"49周\":[0,0,0],\"50周\":[0,0,0],\"51周\":[0,0,0],\"52周\":[0,0,0],\"53周\":[0,0,0]}";

        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcWeekSum()));
    }

    @GetMapping("/statistic8")
    @ResponseBody
    public AjaxResult statistic8()
    {
        String jsonStr = "{\"本周预测总额\":1000,\"本季预测总额\":2200,\"本月预测总额\":700,\"本年预算总额\":2500,\"本年预测总额\":2400,\"本年实际总额\":1940}";
        return AjaxResult.success(JSONObject.toJSONString(alpYingshouService.calcBPAatNow()));
//        return AjaxResult.success(jsonStr);
    }



    /**
     * 应收合同详情
     */
    @GetMapping("/overdueContract")
    public String overdueContract()
    {
        return prefix + "/overdueContract";
    }

    /**
     * 应收合同详情
     */
    @GetMapping("/incomplete")
    public String incomplete()
    {
        return prefix + "/incomplete";
    }

    /**
     * 应收合同详情列表
     */
    @PostMapping("/receivablesContractDetail")
    @ResponseBody
    public TableDataInfo receivablesContractDetail(AlpContractDetail alpContractDetail){
        startPage();
        List<HashMap> list = alpYingshouService.receivablesContractDetail(alpContractDetail);
        return getDataTable(list);
    }

    /**
     * 应收合同详情列表
     */
    @PostMapping("/overdueContractDetail")
    @ResponseBody
    public TableDataInfo overdueContractDetail(AlpContractDetail alpContractDetail){
        startPage();
        List<HashMap> list = alpYingshouService.overdueContractDetail(alpContractDetail);
        return getDataTable(list);
    }


    /**
     * 执行中合同详情列表
     */
    @PostMapping("/incompleteContractDetail")
    @ResponseBody
    public TableDataInfo incompleteContractDetail(AlpContractDetail alpContractDetail){
        startPage();
        List<HashMap> list = alpYingshouService.incompleteContractDetail(alpContractDetail);
        return getDataTable(list);
    }

    /**
     * 应收合同详情查询合同阶段表列表
     */
    @PostMapping("/receivablesContractDetail/stageList")
    @ResponseBody
    public TableDataInfo stageList(Long contractId)
    {
        startPage();
        List<HashMap> list = alpYingshouService.receivablesStageList(contractId);
        return getDataTable(list);
    }

}
